package com.database.dao.impl;

import java.util.List;

import javax.inject.Named;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.database.bean.Game;
import com.database.bean.News;
import com.database.dao.NewsDao;

@Named("newsDao")
@Component
public class NewsDaoImpl extends BaseDaoImpl<News> implements NewsDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public NewsDaoImpl() {
		super(News.class);
	}

	
	@Override
	public List<News> getLastNewsForGame(Game game,Integer amount) {
		Session session = getSession();
		Query q=null;
		if(game!=null){
			q=session.createQuery("FROM News AS N WHERE N.gameId=:gameId ORDER BY N.id DESC");
			q.setParameter("gameId", game.getId());
		}
		else{
			q=session.createQuery("FROM News AS N ORDER BY N.id DESC");
		}
		@SuppressWarnings("unchecked")
		List<News> news=q.list();
		
		if(amount!=null && news.size()>amount){
			news=news.subList(0, amount);
		}
		
		closeSession();
		return news;
	}


}
